/***********************************************************************
 * Module:  AbstractResource.java
 * Author:  wuxinyang
 * Purpose: Defines the Class AbstractResource
 ***********************************************************************/

package com.witframework.plugin.lucene.resource.impl;

import javax.swing.event.EventListenerList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;

import com.witframework.plugin.lucene.index.DocumentListener;
import com.witframework.plugin.lucene.resource.Resource;
import com.witframework.plugin.lucene.resource.ResourceFieldMetaData;
import com.witframework.plugin.lucene.resource.ResourceMetaData;

/** 资源基类
 *
 * @pdOid 13790494-659c-4f7e-9418-c45d114cfcaa */
public abstract class AbstractResource implements Resource {
	private static final Log log = LogFactory	.getLog(AbstractResource.class);
	protected EventListenerList listeners = new EventListenerList();

	public void addDocumentListener(DocumentListener l){
		listeners.add(DocumentListener.class,l);
	}

	public ResourceMetaData getMetaData(String resourceName){
		ResourceMetaData rmd=new ResourceMetaData();
		rmd.setName("user");
		rmd.setHitPattern("url:{}");

		ResourceFieldMetaData rfmd=new ResourceFieldMetaData();
		rfmd.setCode("username");
		rfmd.setIndex("YES");
		rfmd.setStore("COMPRESS");
		rmd.setResourceFieldMetaData(new ResourceFieldMetaData[]{rfmd});

		return rmd;
	}
	public void setDocumentListeners(DocumentListener[] dl) {
		if (dl != null) {
			for (DocumentListener l : dl) {
				addDocumentListener(l);
			}
		}
	}
	public DocumentListener[] getDocumentListeners() {
		return listeners.getListeners(DocumentListener.class);
	}
	protected void fireDocumentExtraction(Document doc) {
		DocumentListener[] ls = getDocumentListeners();
		if (ls != null && ls.length != 0) {
			for (DocumentListener l : ls) {
				l.documentExtracted(doc);
			}
		}
	}
}